package baiqitun.stupro.common.workflow.util;

import org.flowable.engine.HistoryService;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * HistoryService的封装
 * @author kedaji
 */
@Service
public class HistoryUtil {
    @Autowired
    private HistoryService historyService;
    /**
     * 获取某流程历史任务
     * @param processDefinitionKey 流程定义Key
     * @param processInstanceId 流程实例id
     * @return 历史任务
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public HistoricTaskInstance getHistoricTaskByProcessInstanceId(String processDefinitionKey, String processInstanceId){
        return historyService.createHistoricTaskInstanceQuery()
                .processDefinitionKey(processDefinitionKey)
                .processInstanceId(processInstanceId)
                .orderByTaskCreateTime().orderByExecutionId().desc()
                .singleResult();
    }

    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public List<HistoricTaskInstance> listHistoricTaskByOwner(String processDefinitionKey, String owner){
        return historyService.createHistoricTaskInstanceQuery()
                .processDefinitionKey(processDefinitionKey)
                .taskOwner(owner)
                .orderByTaskCreateTime().orderByExecutionId().desc()
                .list();
    }
}
